package com.dzu.easy;

/**
 * @author ZhaoDong
 * @date 2022/11/17 17:37
 * @description 405. 数字转换为十六进制数
 */
public class Test405 {
    public static void main(String[] args) {
        System.out.println(toHex(9));
    }

    public static String toHex(int num) {

        if (num == 0) {
            return "0";
        }
        long numLong = num;
        StringBuilder sb = new StringBuilder();
        if (numLong < 0) {
            numLong = (long) (Math.pow(2, 32) + numLong);
        }
        while (numLong != 0) {
            long u = numLong % 16;
            char c = (char) (u + '0');
            if (u >= 10) {
                c = (char) (u - 10 + 'a');
            }
            sb.append(c);
            numLong /= 16;
        }
        return sb.reverse().toString();

    }

}
